package com.sy.servlet;
import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.SingleThreadModel;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.sy.service.AffixService;

/**
 * 通过uploadify上传附件
 * @author user
 *
 */
@SuppressWarnings({ "deprecation", "serial" })
public class UploadFiletoDB extends HttpServlet implements SingleThreadModel{

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
		AffixService affixService = (AffixService) wac.getBean("affixService");
		UploadParameter uploadParameter=new UploadParameter(request);
		uploadParameter.parseParameters();
		Map<String, String> upFileParameter=uploadParameter.getUpFileParameter();
		String[] fileParameter=uploadParameter.getFileParameter();
		String sb=affixService.uploadFile(upFileParameter.get("classType"),upFileParameter.get("fileId"),upFileParameter.get("projectId"),fileParameter);
		response.setCharacterEncoding("utf-8");
		response.getWriter().print(sb.toString());	
	}
}
